package shuashuami.hb.com.fragment.agent;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import shuashuami.hb.com.adapter.DistributeCenterAdapter;
import shuashuami.hb.com.entity.agent.OrderBean;
import shuashuami.hb.com.entity.agent.PagerBean;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.http.HttpAgentMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.LoginHttp;

/**
 * created by zhangqi at 2017/7/10
 */
public class DistributeCompleteFragm extends Fragment {

    private ListView lv_dist_center_items;
    private int disType = 0;  // 1表示完成订单

    private int currentPage = 1;

    private BaseAdapter adapter;

    private List<OrderBean> mResources;


    public DistributeCompleteFragm() {

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_distribute, container, false);
        initView(view);
        initData();
        initListener();

        return view;
    }

    public void initView(View v) {
        int type = getArguments().getInt("type");

        disType = type;
        lv_dist_center_items = (ListView) v.findViewById(R.id.lv_dist_center_items);

    }

    public void initData() {
        mResources = new ArrayList<>();

        adapter = new DistributeCenterAdapter(getActivity(), mResources);

        lv_dist_center_items.setAdapter(adapter);

        getDataFromSever();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    public void initListener() {

    }


    private void getDataFromSever() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(final String o) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        if (disType == 0) {
                            Log.i("zq", "aaaa");
                        }
                        resolveByJson(o);
                    }
                });


            }
        };

        HttpAgentMethods.getAgentOrders(listener, disType, currentPage);
    }

    public void resolveByJson(String o) {
        PagerBean<OrderBean> pagerBean = new PagerBean<>();
        try {

            JSONObject object = new JSONObject(o);
            int status = object.getInt("status");
            if (status == 203) {
                LoginHttp loginHttp = new LoginHttp(getActivity());
                loginHttp.login();
                return;
            }
            if (status == 200) {
                JSONObject content = object.getJSONObject("content");
                pagerBean.setCount(content.getInt("count"));
                pagerBean.setMaxpage(content.getInt("maxpage"));
                pagerBean.setCurrentpage(content.getInt("currentpage"));
                JSONArray list = content.getJSONArray("list");
                List<OrderBean> items = new ArrayList<>();
                for (int i = 0; i < list.length(); i++) {
                    JSONObject item = list.getJSONObject(i);
                    OrderBean bean = new OrderBean();
                    bean.resolveByJson(item);
                    items.add(bean);
                }

                pagerBean.setList(items);
                mResources.addAll(pagerBean.getList());
                mHandler.sendEmptyMessage(0x1);

            } else {
                String message = object.getString("message");
                Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
            }

        } catch (Exception e) {

        }


    }


    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            if (what == 0x1) {
                adapter.notifyDataSetChanged();
            }
        }
    };
}
